package com.izhaowo.recommend.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by HASEE on 2019/7/21.
 */
public class JavaDateUitl {
    /**
     * 获得该月第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    public static String getNextMonth(String weddingDate) {
        int endMonth = Integer.valueOf(weddingDate.substring(4, 6)) + 1;
        return weddingDate.substring(0, 4) + (endMonth < 10 ? "0" + endMonth : endMonth);
    }

    /**
     * 获得该月最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

    public static String thisMonth() {
        return new SimpleDateFormat("yyyyMM").format(new Date());
    }

    public static String nextMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        //设置日历中月份的最大天数
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
        return new SimpleDateFormat("yyyyMM").format(cal.getTime());
    }


    /**
     * 日期加减计算
     *
     * @param time 最开始时间
     * @param day  天数
     * @return
     */
    public static Date addDay(Date time, int day) {
        Calendar c = Calendar.getInstance();  //开始时间
        //如果time不为空则取现在日期
        c.setTime(time);
        c.add(Calendar.DATE, day);
        return c.getTime();
    }

    public static Date strToDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            return sdf.parse(date);
        } catch (Exception e) {
            System.out.println(" ____  strToDate error" + e.getMessage());
            return null;
        }

    }

    public static Date strToDate(String date, SimpleDateFormat sdf) {
        try {
            return sdf.parse(date);
        } catch (Exception e) {
            System.out.println(" ____  strToDate error" + e.getMessage());
            return null;
        }

    }

    public static String dateToStr(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            return sdf.format(date);
        } catch (Exception e) {
            System.out.println(" ____  dateToStr error" + e.getMessage());
            return null;
        }

    }

    /**
     * 获取某月的天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

}
